package com.ruoyi.web.controller.wexin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.weixin.anno.ILog;
import com.ruoyi.weixin.anno.RequireLogin;
import com.ruoyi.weixin.entity.News;
import com.ruoyi.weixin.entity.R;
import com.ruoyi.weixin.entity.Settings;
import com.ruoyi.weixin.service.NewsService;
import com.ruoyi.weixin.service.SettingsService;
import com.ruoyi.weixin.service.TabbarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (News)表控制层
 *
 * @author makejava
 * @since 2023-12-25 15:19:58
 */

@RestController
@RequestMapping("/wexinLogin/settingNews")
public class NewsController {
    /**
     * 服务对象
     */

    @Autowired
    TabbarService tabbarService;
    @Autowired
    SettingsService settingsService;

    @Resource
    private NewsService newsService;

    @GetMapping("newsList")
    public R newsList(String id) {
        List<Integer> list = new ArrayList<>();
        list.add(0);list.add(Integer.parseInt(id));
        return R.ok(newsService.list(new LambdaQueryWrapper<News>().in(News::getUserId,list)));
    }


    @GetMapping("newsListByUserId")
    public R newsListByUserId(String userId) {
        LambdaQueryWrapper<News> wrapper = new LambdaQueryWrapper<News>();
        List<News> list = newsService.list(wrapper.eq(News::getUserId, userId).eq(News::getStatus, 1));
        wrapper.clear();
        List<News> list1 = newsService.list(wrapper.eq(News::getStatus, 1).eq(News::getUserId, 0));
        list.addAll(list1);
        System.err.println(list);
        return R.ok(list);
    }

    @RequireLogin
    @ILog("已读消息")
    @GetMapping("read")
    public R read(String id) {
        News news = new News();
        news.setId(Integer.parseInt(id));
        news.setStatus("0");
        return R.ok(newsService.updateById(news));
    }

    @RequireLogin
    @PostMapping("settings")
    public R settings(@RequestBody Settings settings) {
        Settings one = settingsService.getOne(new LambdaQueryWrapper<Settings>().eq(Settings::getUserId, settings.getUserId()));
        boolean b;
        if (one!=null){
             b = settingsService.updateById(settings);
        }else {
            settings.setId(null);
             b = settingsService.save(settings);
        }
        return R.ok(b);
    }

    @RequireLogin
    @GetMapping("lunbo")
    public R lunbo() {
        return R.ok(tabbarService.list());
    }

}

